package com.pussinboots.morning.sync.util;

import com.pussinboots.morning.sync.entity.ErpProductView;
import com.pussinboots.morning.sync.entity.Product;
import com.pussinboots.morning.sync.exception.SyncException;
import org.springframework.beans.BeanUtils;

import java.time.LocalDateTime;
import java.util.*;

import static com.pussinboots.morning.sync.exception.ExceptionEnum.LIST_NULL_ERROR;
import static com.pussinboots.morning.sync.exception.ExceptionEnum.MAX_MIN_POISION_ERROR;

/**
 * Created by TaoShuai on 2019/9/22.
 */
public class SyncUtil {
    /**
     * 找出两个List中不同的元素
     * @param list1
     * @param list2
     * @return
     */
    public static List<Long> getDiffrent(List<Long> list1, List<Long> list2) {
        List<Long> diff = new ArrayList<>();
        List<Long> maxList = list1;
        List<Long> minList = list2;
        if (list2.size() > list1.size()) {
            maxList = list2;
            minList = list1;
        }
        Map<Long, Integer> map = new HashMap<>(maxList.size());
        for (Long num : maxList) {
            map.put(num, 1);
        }
        for (Long num : minList) {
            if (map.get(num) != null) {
                map.put(num, 2);
                continue;
            }
            diff.add(num);
        }
        for (Map.Entry<Long, Integer> entry : map.entrySet()) {
            if (entry.getValue() == 1) {
                diff.add(entry.getKey());
            }
        }
        return diff;

    }

    /**
     * 获取两个List中的公共部分，和各不相同的部分
     * @param list1
     * @param list2
     * @return
     */
    public static Map<String, List<String>> breakUpList(List<Map> list1, List<Map> list2) {
        if (list1 == null || list2 == null) {
            throw new SyncException(LIST_NULL_ERROR);
        }

        List<Map> maxList = list1;
        List<Map> minList = list2;
        boolean isList1Max = list1.size() > list2.size();
        if (!isList1Max) {
            maxList = list2;
            minList = list1;
        }
        Map<String, List<String>> map = new HashMap<>();
        List<String> list1Diff = new ArrayList<>();
        List<String> list2Diff = new ArrayList<>();
        List<String> sameList = new ArrayList<>();

        Map<String, Integer> map1 = new HashMap<>(maxList.size());
        Map<Map, Integer> map2 = new HashMap<>(maxList.size());

        for (Map num : maxList) {
            map1.put((String)num.get("erpId"), 1);
            map2.put(num, 1);
        }
        for (Map num : minList) {
            // 此条件成立表示minList与maxList有相同的项
            if (map1.get(num.get("erpId")) != null) {
                sameList.add((String)num.get("erpId"));
                map1.put((String)num.get("erpId"), 2);
                map2.put(num, 2);
                /*if ((num.get("erpId")).equals("d037009")) {
                    System.out.println("拆分方法得出：sameList");
                }*/
                continue;
            }
            if ((Integer)num.get("status") != -1) {
                list2Diff.add((String)num.get("erpId"));
                /*if ((num.get("erpId")).equals("d037009")) {
                    System.out.println("拆分方法得出：list2Diff");
                }*/
            }
        }
        // 挑出maxList中相同项以外的项
        for (Map.Entry<Map, Integer> entry : map2.entrySet()) {
            if (entry.getValue() == 2) {
                continue;
            }
            if ((Integer)entry.getKey().get("status") != -1) {
                list1Diff.add((String)entry.getKey().get("erpId"));
                /*if ((entry.getKey().get("erpId")).equals("d037009")) {
                    System.out.println("拆分方法得出：list1Diff");
                }*/
            }
        }

        if (isList1Max) {
            //System.out.println("正常大小");
            map.put("list1Diff", list1Diff);
            map.put("list2Diff", list2Diff);
        } else {
            //System.out.println("大小转置");
            map.put("list1Diff", list2Diff);
            map.put("list2Diff", list1Diff);
        }

        map.put("sameList", sameList);
        // System.out.println(map);
        return map;
    }

    /**
     * 将erpList分割成需要更新的和需要新增的两部分
     * @param erpList
     * @param sameList
     * @return
     */
    public static Map<String, List<ErpProductView>> breakUpErpList(List<ErpProductView> erpList, List<String> sameList, List<String> erpProductDiff) {
        Map<String, List<ErpProductView>> map = new HashMap<>();
        List<ErpProductView> updateList = new ArrayList<>();
        List<ErpProductView> insertList = new ArrayList<>();
        if (sameList == null || sameList.isEmpty()) {
            insertList = erpList;
        }
        /*for (Long num : sameList) {
            for (ErpProductView p : erpList) {
                if (p.getErpId().longValue() == num.longValue()) {
                    updateList.add(p);
                    break;
                }
                insertList.add(p);
            }
        }*/
        else {

            for (ErpProductView p : erpList) {
                boolean isSameMatch = false;
                for (int i=0; i<sameList.size(); i++) {
                    if (!sameList.isEmpty() && p.getErpId().equals(sameList.get(i)) ) {
                        updateList.add(p);
                        sameList.remove(i);
                        i--;
                        isSameMatch = true;
                        break;
                    }
                }
                if (!isSameMatch) {
                    insertList.add(p);
                }
            }
        }

        map.put("updateList", updateList);
        map.put("insertList", insertList);
        return map;
    }

    public static List<Product> transformList(List<ErpProductView> erpProductViews){
        List<Product> productList = new ArrayList<>();
        Product product;
        for (ErpProductView erpInsertView : erpProductViews) {
            /*if (erpInsertView.getErpId().equals("d037009")) {
                System.out.println("更新转换 d037009====>" + erpInsertView.toString());
            }*/
            product = Product.builder().build();
            BeanUtils.copyProperties(erpInsertView, product);
            product.setStatus(1);
            productList.add(product);
            /*if (product.getErpId().equals("d037009")) {
                System.out.println("转换后 product====>" + erpInsertView.toString());
            }*/

        }
        return productList;
    }

    public static void main(String[] args) {
        /*List<Long> maxList = new ArrayList<>();
        List<Long> minList = new ArrayList<>();
        Long[] nums1 = {1L, 2L, 3L, 4L, 5L, 6L};
        Long[] nums2 = { 4L, 5L, 7L};
        breakUpList(Arrays.asList(nums2), Arrays.asList(nums1));*/
        System.out.println(LocalDateTime.now());
    }
}
